<?php

/**
 * ECSHOP 商品类型管理程序
 * ============================================================================
 * * 版权所有 2005-2012 上海商派网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.ecshop.com；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: liubo $
 * $Id: goods_type.php 17217 2011-01-19 06:29:08Z liubo $
*/

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');require_once(ROOT_PATH . "includes/fckeditor/fckeditor.php");require_once(ROOT_PATH . 'includes/cls_image.php');require_once(ROOT_PATH . '/includes/oss-sdk/Object.php');$image = new cls_image();/* 允许上传的文件类型 *///$allow_file_types = '|GIF|JPG|PNG|BMP|SWF|DOC|XLS|PPT|MID|WAV|ZIP|RAR|PDF|CHM|RM|TXT|';$allow_file_types = '|GIF|JPG|JEPG|PNG|BMP|SWF|';// 最大上传文件大小$php_maxsize = ini_get('upload_max_filesize');$htm_maxsize = '2M';

$exc = new exchange($ecs->table("copyright"), $db, 'copyright_id', 'copyright_content');

/*------------------------------------------------------ */
//-- 管理界面
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'manage')
{	admin_priv('technology_manage');
    assign_query_info();

    $smarty->assign('ur_here',          $_LANG['copyright']);
    $smarty->assign('full_page',        1);

    $copyright_list = get_copyright_list();
    $good_in_type = '';
	foreach($copyright_list['type'] as $key => $var){		$copyright_list['type'][$key]['img_url'] = empty($copyright_list['type'][$key]['img_url']) ? '' :            OSS_HOST.$copyright_list['type'][$key]['img_url'];
//		$copyright_list['type'][$key]['technology_read']=mb_substr($copyright_list['type'][$key]['technology_read'], 0,15,'utf-8');
//		$copyright_list['type'][$key]['technology_picture'] = empty($copyright_list['type'][$key]['technology_picture']) ? '' :
//          '<a href="'.OSS_HOST.$technology_list['type'][$key]['technology_picture'].'" target="_brank"><img src="images/picflag.gif" width="16" height="16" border="0"  /></a>';
//		$technology_list['type'][$key]['technology_picture']=OSS_HOST.$technology_list['type'][$key]['technology_picture'];
	}
    $smarty->assign('copyright_list',   $copyright_list['type']);
    $smarty->assign('filter',       $copyright_list['filter']);
    $smarty->assign('record_count', $copyright_list['record_count']);
    $smarty->assign('page_count',   $copyright_list['page_count']);
	
    $smarty->assign('action_link',      array('text' => $_LANG['add_copyright'], 'href' => 'copyright.php?act=add'));

    $smarty->display('copyright_list.htm');
}

/*------------------------------------------------------ */
//-- 获得列表
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'query')
{
    $copyright_list = get_copyright_list();
	foreach($copyright_list['type'] as $key => $var){		$copyright_list['type'][$key]['img_url'] = empty($copyright_list['type'][$key]['img_url']) ? '' :            OSS_HOST.$copyright_list['type'][$key]['img_url'];//		$copyright_list['type'][$key]['technology_read']=mb_substr($copyright_list['type'][$key]['technology_read'], 0,15,'utf-8');//		$copyright_list['type'][$key]['technology_picture'] = empty($copyright_list['type'][$key]['technology_picture']) ? '' ://          '<a href="'.OSS_HOST.$technology_list['type'][$key]['technology_picture'].'" target="_brank"><img src="images/picflag.gif" width="16" height="16" border="0"  /></a>';//		$technology_list['type'][$key]['technology_picture']=OSS_HOST.$technology_list['type'][$key]['technology_picture'];	}	
    $smarty->assign('copyright_list',   $copyright_list['type']);
    $smarty->assign('filter',       $copyright_list['filter']);
    $smarty->assign('record_count', $copyright_list['record_count']);
    $smarty->assign('page_count',   $copyright_list['page_count']);

    make_json_result($smarty->fetch('copyright_list.htm'), '',
        array('filter' => $copyright_list['filter'], 'page_count' => $copyright_list['page_count']));
}

/*------------------------------------------------------ */
//-- 修改商品类型名称
/*------------------------------------------------------ */
elseif ($_REQUEST['act'] == 'edit_type_name')
{
    check_authz_json('technology_manage');

    $type_id   = !empty($_POST['id'])  ? intval($_POST['id']) : 0;
    $type_name = !empty($_POST['val']) ? json_str_iconv(trim($_POST['val']))  : '';

    /* 检查名称是否重复 */
    $is_only = $exc->is_only('cat_name', $type_name, $type_id);

    if ($is_only)
    {
        $exc->edit("cat_name='$type_name'", $type_id);

        admin_log($type_name, 'edit', 'goods_type');

        make_json_result(stripslashes($type_name));
    }
    else
    {
        make_json_error($_LANG['repeat_type_name']);
    }
}

/*------------------------------------------------------ */
//-- 切换启用状态
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'toggle_enabled')
{
    check_authz_json('technology_manage');

    $id     = intval($_POST['id']);
    $val    = intval($_POST['val']);

    $exc->edit("enabled='$val'", $id);

    make_json_result($val);
}

/*------------------------------------------------------ */
//-- 添加商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'add')
{
    admin_priv('technology_manage');


	$copyright_type = $db->getAll("SELECT copyright_type_id,copyright_name from ".$GLOBALS['ecs']->table('copyright_type'));	$smarty->assign('copyright_type',   $copyright_type);
        $smarty->assign('ur_here',     $_LANG['new_copyright']);

    $smarty->assign('action_link', array('href'=>'copyright.php?act=manage', 'text' => $_LANG['copyright_list']));

    $smarty->assign('action',      'add');

    $smarty->assign('form_act',    'insert');

    $smarty->assign('picture_type',  array('enabled' => 1));



    assign_query_info();
    $smarty->display('copyright_info.htm');
}

elseif ($_REQUEST['act'] == 'insert')
{
	$copyright_name['copyright_type'] = sub_str($_POST['copyright_id'], 0);	$copyright_name['copyright_content'] = sub_str($_POST['copyright_content'], 0);	$copyright_name['cat_type'] = $_POST['cat_type'];	$copyright_name['copyright_replace'] = $_POST['copyright_replace'];	// 图验证	 if (isset($_FILES['copyright_picture']['error'])) // php 4.2 版本才支持 error    {    			if ($_FILES['copyright_picture']['error'] == 0)        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {               sys_msg($_LANG['img_type']);            }        }    	 if ($_FILES['copyright_picture']['error'] == 1)        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {               sys_msg(sprintf($_LANG['img_url_too_big'],$php_maxsize), 1,  array(), false);            }        }        elseif ($_FILES['copyright_picture']['error'] == 2)        {            sys_msg(sprintf($_LANG['img_url_too_big'], $php_maxsize), 1, array(), false);        }    }else{    	if ($_FILES['copyright_picture']['tmp_name'] != 'none')        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {                 sys_msg($_LANG['img_type']);;            }        }    }	/* 取得文件地址 */    if ((isset($_FILES['copyright_picture']['error']) && $_FILES['copyright_picture']['error'] == 0) || (!isset($_FILES['copyright_picture']['error']) && isset($_FILES['copyright_picture']['tmp_name']) && $_FILES['copyright_picture']['tmp_name'] != 'none'))    {//      // 检查文件格式//      if (!check_file_type($_FILES['file']['tmp_name'], $_FILES['file']['name'], $allow_file_types))//      {//          sys_msg($_LANG['invalid_file']);//      }        // 复制文件        $res = check_tech_file($_FILES['copyright_picture']);        if ($res != false)        {            $file_url = $res;		    /* 上传图片到oss*/						uploadFile($ossClient, $bucket, $file_url, $_FILES['copyright_picture']['tmp_name'],$_FILES['copyright_picture']['type']);				        }    }	if($file_url==""){		$file_url = $_POST['file_url'];	}	$copyright_name['img_url']=$file_url;
    if ($db->autoExecute($ecs->table('copyright'), $copyright_name) !== false)
    {		
        $links = array(array('href' => 'copyright.php?act=manage', 'text' => $_LANG['copyright_list']));				admin_log($copyright_name['copyright_name'], 'add', 'copyright');		
        sys_msg('新建版权声明成功', 0, $links);
    }
    else
    {
        sys_msg('新建版权声明失败', 1);		
    }
}

/*------------------------------------------------------ */
//-- 编辑商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'edit')
{	admin_priv('technology_manage');
    $copyright = get_copyright(intval($_GET['copyright_id']));	if($copyright['img_url'] ==""){			}else{		$copyright['old_img_url']=$copyright['img_url'];		$copyright['img_url']=OSS_HOST.$copyright['img_url'];
			}

    if (empty($copyright))
    {
        sys_msg($_LANG['cannot_found_copyright'], 1);
    }
	$copyright_type = $db->getAll("SELECT copyright_type_id,copyright_name from ".$GLOBALS['ecs']->table('copyright_type'));		$smarty->assign('copyright_type',   $copyright_type);	
    $smarty->assign('ur_here',     $_LANG['edit_copyright']);
    $smarty->assign('action_link', array('href'=>'copyright.php?act=manage', 'text' => $_LANG['copyright_list']));
    $smarty->assign('action',      'add');
    $smarty->assign('form_act',    'update');
    $smarty->assign('copyright',  $copyright);

    assign_query_info();
    $smarty->display('copyright_info.htm');
}
elseif ($_REQUEST['act'] == 'update')
{
	$copyright_name['copyright_type'] = $_POST['copyright_id'];	$copyright_name['copyright_content'] = $_POST['copyright_content'];	$copyright_name['cat_type'] = $_POST['cat_type'];	$copyright_name['copyright_replace'] = $_POST['copyright_replace'];	$copyright_id = $_POST['id'];	// 图验证	 if (isset($_FILES['copyright_picture']['error'])) // php 4.2 版本才支持 error    {    			if ($_FILES['copyright_picture']['error'] == 0)        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {               sys_msg($_LANG['img_type']);            }        }    	 if ($_FILES['copyright_picture']['error'] == 1)        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {               sys_msg(sprintf($_LANG['img_url_too_big'],$php_maxsize), 1,  array(), false);            }        }        elseif ($_FILES['copyright_picture']['error'] == 2)        {            sys_msg(sprintf($_LANG['img_url_too_big'], $php_maxsize), 1, array(), false);        }    }else{    	if ($_FILES['copyright_picture']['tmp_name'] != 'none')        {            if (!$image->check_img_type($_FILES['copyright_picture']['type']))            {                 sys_msg($_LANG['img_type']);;            }        }    }	/* 取得文件地址 */    if ((isset($_FILES['copyright_picture']['error']) && $_FILES['copyright_picture']['error'] == 0) || (!isset($_FILES['copyright_picture']['error']) && isset($_FILES['copyright_picture']['tmp_name']) && $_FILES['copyright_picture']['tmp_name'] != 'none'))    {//      // 检查文件格式
//      if (!check_file_type($_FILES['file']['tmp_name'], $_FILES['file']['name'], $allow_file_types))
//      {
//          sys_msg($_LANG['invalid_file']);
//      }	    $copyright_picture = $exc->get_name($copyright_id,'img_url');		if ($copyright_picture)		{			/* 删除oss上的图片 */							deleteObject($ossClient, $bucket, $copyright_picture);							}        // 复制文件        $res = check_tech_file($_FILES['copyright_picture']);        if ($res != false)        {            $file_url = $res;		    /* 上传图片到oss*/						uploadFile($ossClient, $bucket, $file_url, $_FILES['copyright_picture']['tmp_name'],$_FILES['copyright_picture']['type']);				        }    }	if($file_url==""){		$file_url = $_POST['file_url'];	}	$copyright_name['img_url']=$file_url;
    if ($db->autoExecute($ecs->table('copyright'), $copyright_name, 'UPDATE', "copyright_id='$copyright_id'") !== false)
    {
        $links = array(array('href' => 'copyright.php?act=manage', 'text' => $_LANG['copyright_list']));		admin_log($copyrigh_name['copyright_name'], 'edit', 'copyright');		
        sys_msg('版权声明更新成功', 0, $links);
    }
    else
    {
        sys_msg('版权声明更新失败', 1);
    }
}

/*------------------------------------------------------ */
//-- 删除商品类型
/*------------------------------------------------------ */

elseif ($_REQUEST['act'] == 'remove')
{
    check_authz_json('technology_delect');
//	admin_priv('technology_delect');
    $id = intval($_GET['id']);
//	$name = $exc->get_name($id,'technology_name');
    $copyright_picture = $exc->get_name($id,'img_url');
    if ($exc->drop($id))
    {//      admin_log(addslashes($name), 'remove', 'technology');
		if ($copyright_picture)		{			/* 删除oss上的图片 */							deleteObject($ossClient, $bucket, $copyright_picture);							}		//		 $GLOBALS['db']->query("DELETE FROM " .$ecs->table('picture_type'). " WHERE picture_id =" .$id );
        /* 清除该类型下的所有属性 */
//      $sql = "SELECT attr_id FROM " .$ecs->table('attribute'). " WHERE cat_id = '$id'";
//
//      $arr = $db->getCol($sql);
//
//
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('attribute'). " WHERE attr_id " . db_create_in($arr));
//
//      $GLOBALS['db']->query("DELETE FROM " .$ecs->table('goods_attr'). " WHERE attr_id " . db_create_in($arr));

        $url = 'copyright.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);

        ecs_header("Location: $url\n");
        exit;
    }
//
    else

    {

        make_json_error($_LANG['remove_failed']);

    }
}

/**
 * 获得所有商品类型
 *
 * @access  public
 * @return  array
 */
function get_copyright_list()
{
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();
//		$filter['cat_id']           = empty($_REQUEST['cat_id']) ? -1 : intval($_REQUEST['cat_id']);
//
//      $filter['technology_type']       = empty($_REQUEST['technology_type']) ? -1 : intval($_REQUEST['technology_type']);
//      
//		$filter['brand_type']       = empty($_REQUEST['brand_type']) ? -1 : intval($_REQUEST['brand_type']);
//
//     	$filter['brand_id']         = empty($_REQUEST['brand_id']) ? -1 : intval($_REQUEST['brand_id']);
//     	
//		$filter['keyword']          = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
//
//		$where= "";
//		if($filter['cat_id']!=-1){
//			$where .= " AND t.technology_cat='$filter[cat_id]'";
//			
//		}
//		if($filter['technology_type']!=-1){
//			if($filter['technology_type']==1){
//				$where .= " AND t.cat_type=0";
//			}else{
//				$where .= " AND t.cat_type=1";
//			}
//			
//		}
//		if($filter['brand_type']!=-1){
//			if($filter['brand_type']==1){
//				$where .= " AND t.technology_type=0";
//			}else{
//				$where .= " AND t.technology_type=1";
//			}
//			
//			
//		}
//		if($filter['brand_id']!=-1){
//			$where .= " AND t.brand_id='$filter[brand_id]'";
//		}
//		        /* 关键字 */
//
//      if (!empty($filter['keyword']))
//
//      {
//
//          $where .= " AND (t.technology_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%' )";
//
//      }        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['ecs']->table('copyright');
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);

        /* 查询记录 *///      $sql = "SELECT t.*, a.brand_name ".
//
//             "FROM ". $GLOBALS['ecs']->table('technology'). " AS t ".
//
//             "LEFT JOIN ". $GLOBALS['ecs']->table('brand'). " AS a ON a.brand_id=t.brand_id ".
//
//             " where t.technology_id!=0 $where ".
//
//             'LIMIT ' . $filter['start'] . ',' . $filter['page_size'];				$sql=" select c.*,ct.copyright_name from ".$GLOBALS['ecs']->table('copyright')." as c ".		" left join ".$GLOBALS['ecs']->table('copyright_type')." as ct on ct.copyright_type_id=c.copyright_type ".		" limit ". $filter['start'] . ',' . $filter['page_size'];
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }

    $all = $GLOBALS['db']->getAll($sql);
    return array('type' => $all, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
}

/**
 * 获得指定的商品类型的详情
 *
 * @param   integer     $cat_id 分类ID
 *
 * @return  array
 */
function get_copyright($copyright_id)
{
    $sql = "SELECT * FROM " .$GLOBALS['ecs']->table('copyright'). " WHERE copyright_id='$copyright_id'";

    return $GLOBALS['db']->getRow($sql);
}
/* 上传文件 */function check_tech_file($upload){    $filename = cls_image::random_filename() . substr($upload['name'], strpos($upload['name'], '.'));//  $path     = ROOT_PATH. DATA_DIR . "/article/" . $filename;    return DATA_DIR . "/copyright/" . $filename;}
?>
